package com.ezcolour.ticket.web.thread;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ezcolour.ticket.common.util.CrawlerUtil;
import com.ezcolour.ticket.web.global.GlobalCache;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.concurrent.ConcurrentHashMap;

public class TicketThread extends Thread{
    private String vocalId;
    private String performId;
    private String username;

    public TicketThread(String vocalId,String performId,String username) {
        this.vocalId = vocalId;
        this.performId = performId;
        this.username = username;
    }


    public void run(){
        long time = new Date().getTime() + 300000;
        boolean result = true;
//        System.out.println("线程开始"+ this.username +";"+ this.password);
        while (result){
            long now = new Date().getTime();
            if(now >= time  ){
                return;
            }
            ConcurrentHashMap<String, String> userMap = GlobalCache.user_auto_all.get(this.username);
            if(userMap!=null && userMap.size()>=0){
                String p = userMap.get(this.vocalId);
                if(StringUtils.isNotEmpty(p)){
                    return;
                }
            }
            try{
                String priceList = CrawlerUtil.getPriceList(this.vocalId, this.performId);
                JSONObject jsonObj = JSONObject.parseObject(priceList);
                JSONObject data = (JSONObject) jsonObj.get("Data");
                JSONArray prices = (JSONArray)data.get("list");
                JSONObject price = null;
                for (Object good:
                prices) {
                    price =(JSONObject)good;
                    Integer statu = (Integer)price.get("Status");
                    System.out.println("projectId:"+this.vocalId+"；performId:"+this.performId+"；id:"+price.get("PriceID").toString()+"；状态："+statu);
                    //如果statu等于0 则有货
                    switch (statu){
                        case 0 :
                            userMap = GlobalCache.user_auto_all.get(this.username);
                            if(userMap!=null && userMap.size()>=0){
                                String p = userMap.get(this.vocalId);
                                if(StringUtils.isNotEmpty(p)){
                                    return;
                                }else{
                                    userMap.put(this.vocalId,price.get("PriceID").toString());
                                    GlobalCache.user_auto_all.put(this.username,userMap);
                                }
                            }else{
                                userMap = new ConcurrentHashMap<String, String>();
                                userMap.put(this.vocalId,price.get("PriceID").toString());
                                GlobalCache.user_auto_all.put(this.username,userMap);
                            }
                            result = false;
                            break;
                        default:
                            result = true;
                            break;
                    }
                }
                Thread.sleep(1000);                 //1000 毫秒，也就是1秒.
            }catch (Exception e){
                e.printStackTrace();
            }


        }
    }


    public String getVocalId() {
        return vocalId;
    }

    public void setVocalId(String vocalId) {
        this.vocalId = vocalId;
    }

}
